//
//  ComponentManager.swift
//  
//
//  Created by tank on 2021/9/29.
//

import Foundation

/// Component manager
public class ComponentManager {
    
    static public let shared = ComponentManager()
    
    public private(set) var componentClasses: [String: ComponentProtocol.Type] = [:]
    
    private init() {
        
    }
    
    /// 注册组件
    /// - Parameter component: 组件类
    public func register(component: ComponentProtocol.Type) {
        
        componentClasses[component.type] = component
    }
    
    public func component(type: String,
                          id: String,
                          value: Any?,
                          page: PageProtocol) -> ComponentProtocol? {
        
        guard let componentClass = componentClasses[type] else {
            return nil
        }
        
        return componentClass.init(id: id, value: value, page: page)
    }
}
